package com.sxtyxt.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.widget.ScrollView;

import com.sxtyxt.online.App;
import com.sxtyxt.online.SystemEnv;
import com.sxtyxt.util.UIUtil;

/**
 * Created by ysnow on 2015/4/20.
 */
public class YsnowScrollViewPageOne extends ScrollView {
    private int mScreenHeight;
    public float oldY;
    private int t;

    private Context context;
    private GestureDetector mGestureDetector;

    public YsnowScrollViewPageOne(Context context) {
        this(context, null);
        this.context = context;
    }

    public YsnowScrollViewPageOne(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public YsnowScrollViewPageOne(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics metrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(metrics);
        int statusBarHeight = SystemEnv.getStatusBarHeight();
        mScreenHeight = metrics.heightPixels
                - UIUtil.dip2px(App.getInstance().getApplicationContext(), 122)
                - statusBarHeight;//得到屏幕的高度(像素)
        mGestureDetector = new GestureDetector(context, new YScrollDetector());
    }


    @Override
    public boolean onTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {
            case MotionEvent.ACTION_MOVE:
                //在滑动的时候获得当前值，并计算得到YS,用来判断是向上滑动还是向下滑动
                float Y = ev.getY();
                float Ys = Y - oldY;
                //focus
                this.requestDisallowInterceptTouchEvent(false);
                //得到scrollview里面空间的高度
                int childHeight = this.getChildAt(0).getMeasuredHeight();
                //子控件高度减去scrollview向上滑动的距离
                int padding = childHeight - t;
                //Ys<0表示手指正在向上滑动，padding==mScreenHeight表示本scrollview已经滑动到了底部
//                System.out.println("padding:" + padding);
//                System.out.println("mScreenHeight" + mScreenHeight);
                if (Ys < 0 && padding == mScreenHeight) {

                    //让顶级的scrollview重新获得滑动事件
                    getParent().getParent().requestDisallowInterceptTouchEvent(false);

                } else {
                    this.requestDisallowInterceptTouchEvent(false);
                }
                break;
            case MotionEvent.ACTION_DOWN:
                //手指按下的时候，获得滑动事件，也就是让顶级scrollview失去滑动事件
                getParent().getParent().requestDisallowInterceptTouchEvent(true);
                this.requestDisallowInterceptTouchEvent(false);
                //并且记录Y点值
                oldY = ev.getY();
                break;
            case MotionEvent.ACTION_UP:
                getParent().getParent().requestDisallowInterceptTouchEvent(true);
                this.requestDisallowInterceptTouchEvent(false);

                break;


        }

        return super.onTouchEvent(ev);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        //t表示本scrollview向上滑动的距离
        this.t = t;
        super.onScrollChanged(l, t, oldl, oldt);
    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        //手指按下的时候，获得滑动事件，也就是让顶级scrollview失去滑动事件
        getParent().getParent().requestDisallowInterceptTouchEvent(true);
        return super.onInterceptTouchEvent(ev)
                && mGestureDetector.onTouchEvent(ev);
    }

    class YScrollDetector extends GestureDetector.SimpleOnGestureListener {

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2,
                                float distanceX, float distanceY) {
            /**
             * 如果我们滚动更接近水平方向,返回false,让子视图来处理它
             */
            return (Math.abs(distanceY) > Math.abs(distanceX));
        }
    }


}
